<?php
/**
 * Created by IDEA.
 * User: dinesh
 * Date: Apr 19, 2012
 * Time: 10:23:51 PM
 * To change this template use File | Settings | File Templates.
 */

class Employ {
    private $db;
    private $id;
    private $fullName;
    private $initName;
    private $title;
    private $email;
    private $instituteId;
    private $sectionId;
    private $categoryId;
    private $designation;
    private $nic;
    private $epfNo;
    private $gender;
    private $birthday;
    private $address_p1;
    private $address_p2;
    private $address_p3;
    private $address_t1;
    private $address_t2;
    private $address_t3;
    private $phoneMobile;
    private $phoneHome;
    private $phoneOffice;
    private $pic_src;
    private $status;
    private $isDelete;
    private $version;
    private $instituteMenu;
    private $categoryMenu;
    private $sectionMenu;

    function __set($name, $value) {
        // TODO: Implement __set() method.
        $this->$name = $value;

    }

    function __get($name) {
        // TODO: Implement __get() method.
        return $this->$name;
    }

    public function __construct() {
        $ado = new AdodbDriver();
        $this->db = $ado->pConnect(true);
        $this->db->debug = false;
        $this->setInstituteArray();
        $this->setCategoryArray();
    }

    public function setInstituteArray() {
        $sql = "SELECT id,name from aea_institute order by id asc";
        $dep_code = $this->db->Execute($sql);
        $this->instituteMenu = $dep_code->GetAssoc();

    }

    public function setCategoryArray() {
        $sql = "SELECT id,name from aea_employ_category order by id asc";
        $categoryNames = $this->db->Execute($sql);
        $this->categoryMenu = $categoryNames->GetAssoc();

    }

    public function getGrid($posStart, $count, $instituteId, $section_id, $name, $nic, $sort_by, $sort_dir) {
        $sql = "SELECT  id,category_id,institute_id,section_id,full_name,init_name,nic,email,status FROM aea_profile Where is_delete=0 ";
        if ($instituteId != '')
            $sql .= " and institute_id = '$instituteId'";
        if ($name != '')
            $sql .= " and full_name like '$name%'";
        if ($nic != '')
            $sql .= " and nic like '$nic%'";
        if ($section_id != '')
            $sql .= " and section_id = '$section_id'";
        if ($sort_dir == '')
            $sort_dir = "asc";
        if ($sort_by != '')
            $sql .= " Order By $sort_by $sort_dir";
        if ($posStart == 0) {
            $sqlCount = "Select count(*) as cnt from ($sql) as tbl";

            $resCount = $this->db->Execute($sqlCount);
            if (!$resCount)
                print $this->db->ErrorMsg();
            else
                while (!$resCount->EOF) {
                    $totalCount = $resCount->fields[0];
                    $resCount->MoveNext();
                }

        }
        $sql .= " LIMIT " . $posStart . "," . $count;
        $res = $this->db->Execute($sql);
        if ($res) {
            $xmlString = "<?xml version=\"1.0\"  encoding=\"ISO-8859-1\"?><rows total_count='" . $totalCount . "' pos='" . $posStart . "'>";
            while (!$res->EOF) {
                $xmlString .= "<row id='" . $res->fields['id'] . "'>
                    <cell>" . $res->fields['id'] . "</cell>
                   <cell>" . $res->fields['full_name'] . "</cell>
                    <cell>" . $res->fields['init_name'] . "</cell>
                    <cell>" . $res->fields['nic'] . "</cell>
                    <cell>" . $this->instituteMenu[$res->fields['institute_id']] . "</cell>
                    <cell>" . $this->instituteMenu[$res->fields['section_id']] . "</cell>
                    <cell>" . $res->fields['email'] . "</cell>
                    <cell>" . ($res->fields['status'] == 1 ? "Active" : "Incative") . "</cell>
                    <cell>" . $res->fields['status'] . "</cell>
                    <cell>" . $res->fields['institute_id'] . "</cell>
                    <cell>" . $res->fields['section_id'] . "</cell>
                    <cell>" . $this->categoryMenu[$res->fields['category_id']] . "</cell>
                    </row>";
                $posStart++;
                $res->MoveNext();
            }
            $xmlString .= "</rows>";
        } else {
            $xmlString = mysql_errno() . ": " . mysql_error() . " at " . __LINE__ . " line in " . __FILE__ . " file<br>" . $sql;
        }

        return $xmlString;
    }

    public function save() {
        ADOdb_Active_Record::SetDatabaseAdapter($this->db);
        try {

            $employAR = new EmployAR();

            if ($this->id != '' AND $this->id != NULL) {
                $employAR->Load("id=$this->id");
            } else {
                $employAR->create_time = date('Y-m-d H:i:s', time());
                $employAR->create_user_id = $_SESSION['user_id'];
                $employAR->section_id = $this->sectionId;
                $employAR->institute_id = $this->instituteId;


            }

            $employAR->full_name = $this->fullName;
            $employAR->init_name = $this->initName;
            $employAR->title = $this->title;
            $employAR->nic = $this->nic;
            $employAR->epf_no = $this->epfNo;
            $employAR->email = $this->email;
            $employAR->gender = $this->gender;
            $employAR->birthday = $this->birthday;
            $employAR->category_id = $this->categoryId;

            $employAR->update_user_id = $_SESSION['user_id'];
            $employAR->designation = $this->designation;
            $employAR->phone_mobile = $this->phoneMobile;
            $employAR->phone_home = $this->phoneHome;
            $employAR->phone_office = $this->phoneOffice;
            $employAR->p_address_line1 = $this->address_p1;
            $employAR->p_address_line2 = $this->address_p2;
            $employAR->p_address_line3 = $this->address_p3;
            $employAR->t_address_line1 = $this->address_t1;
            $employAR->t_address_line2 = $this->address_t2;
            $employAR->t_address_line3 = $this->address_t3;
            $employAR->picture_src = $this->pic_src;
            $employAR->status = $this->status;
            $employAR->is_delete = 0;
            $employAR->version = 0;
            $i = $employAR->save();

            if (is_bool($i)) {
                if ($i) {

                    $empIns = new EmpInsAR();
                    $empIns->emp_id = $employAR->id;
                    $empIns->institute_id = $employAR->institute_id;
                    $empIns->section_id = $employAR->section_id;
                    $empIns->category_id = $employAR->category_id;
                    $empIns->from_date = date('Y-m-d', time());
                    $empIns->to_date = '0000-00-00';
                    $empIns->create_time = date('Y-m-d H:i:s', time());
                    $empIns->create_user_id = $_SESSION['user_id'];
                    $empIns->update_user_id = $_SESSION['user_id'];
                    $j = $empIns->save();
                }
            }

        }
        catch (ADODB_Exception $adodb) {
            echo  $adodb->getMessage();
        }
        catch (exception $e) {
            //var_dump($e);
            // print_r($e);
            adodb_backtrace($e->gettrace());
        }
        return $i;
    }

    public function edit() {
        ADOdb_Active_Record::SetDatabaseAdapter($this->db);
        try {
            $profile = new Profile();
            if ($this->profile_id != '' AND $this->profile_id != NULL) {
                $profile->Load("id=$this->profile_id");
            } else {
                return 0;
            }
            $profile->full_name = $this->fullName;
            $profile->init_name = $this->initName;
            $profile->nic = $this->nic;
            $profile->email = $this->email;
            $profile->gender = $this->gender;
            $profile->birthday = $this->birthday;
            $profile->theme = $this->themel;
            $profile->update_user_id = $_SESSION['user_id'];
            $i = $profile->save();
        } catch (exception $e) {
            print_r($e);
            adodb_backtrace($e->gettrace());
        }
        return $i;
    }

    public function getEmploy($empId) {
        $sql = "SELECT  * FROM aea_profile Where id='$empId' LIMIT 1";
        $res = $this->db->Execute($sql);

        return $res;
    }

    public function validateNic($nic, $id) {
        $sql = "SELECT  id FROM aea_profile Where id!= '$id' AND UPPER(nic)=UPPER('$nic') LIMIT 1";
        $recordSet = $this->db->Execute($sql);
        return !$recordSet->FetchRow();
    }

    public function validatePassword($id, $password) {
        $sql = "SELECT  * FROM aea_user Where id=$id AND password=SHA1('$password') LIMIT 1";
        $recordSet = $this->db->Execute($sql);
        return $recordSet->FetchRow();

    }

    public function resetPassword() {
        ADOdb_Active_Record::SetDatabaseAdapter($this->db);
        try {
            if ($this->id != '' AND $this->id != NULL) {
                $user = new UserAR();
                $user->Load("id=$this->id");
                $user->password = SHA1($this->password);
                $i = $user->save();
            }
        } catch (exception $e) {
            //var_dump($e);
            print_r($e);
            adodb_backtrace($e->gettrace());
            return print_r($e);
        }
        return $i;

    }

    public function updateInstitute($id, $empId, $instituteId, $sectionId, $categoryId, $oldToDate, $fromDate) {
        $sql = "UPDATE aea_employ_institute SET  to_date='$oldToDate' WHERE id='$id' ";
        $updateToDateRS = $this->db->Execute($sql);

        $sql = "UPDATE aea_profile SET  institute_id='$instituteId',section_id='$sectionId', category_id='$categoryId' WHERE id='$empId' ";
        $updateEmployRS = $this->db->Execute($sql);
        $createTime = date('Y-m-d H:i:s', time());
        $createUserID = $_SESSION['user_id'];
        $sql = "INSERT INTO aea_employ_institute
                    (emp_id, institute_id, section_id, category_id,from_date, create_user_id,create_time,is_delete)
                VALUES
                ('$empId', '$instituteId', '$sectionId','$categoryId','$fromDate','$createUserID','$createTime',0)";
        $addEmployRS = $this->db->Execute($sql);
        return $addEmployRS;
    }

    public function getEmpIns($empId, $insId, $secId) {
        $sql = "SELECT  id,from_date FROM aea_employ_institute Where emp_id='$empId' AND institute_id='$insId' AND section_id='$secId' AND to_date='0000-00-00'  LIMIT 1";
        $res = $this->db->Execute($sql);
        $sql = "SELECT  * FROM aea_employ_institute Where emp_id='$empId'";
        $resAll = $this->db->Execute($sql);
        if ($resAll->RecordCount() > 0) {
            $innerHtml = "<table class='gradienttable'><tr><th width='250px'>Institute Name</th><th width='250px'>Section Name</th><th width='150px'>Category</th> <th width='120px'>From Date</th><th width='120px'>To Date</th></tr>";
            while (!$resAll->EOF) {
                $innerHtml .= "<tr>
                   <td>" . $this->instituteMenu[$resAll->fields['institute_id']] . "</td>
                   <td>" . $this->instituteMenu[$resAll->fields['section_id']] . "</td>
                   <td>" . $this->categoryMenu[$resAll->fields['category_id']] . "</td>
                   <td>" . $resAll->fields['from_date'] . "</td>
                   <td>" . $resAll->fields['to_date'] . "</td>
                   </tr>";
                $resAll->MoveNext();
            }
            $innerHtml .= "</table>";
        } else {
            $innerHtml = "No Records Found !";
        }
        $data = array('fromDate' => $res, 'tableHtml' => $innerHtml);
        return $data;
    }

    public function actionGetThemeListMenu() {
        $sql = 'select theme_name,id from aea_theme';
        $themeMenuList = $this->db->Execute($sql);
        return $themeMenuList->GetMenu2('theme', '', '0:--selected--', '', '', 'id="theme"');
    }

    public function delete() {
        ADOdb_Active_Record::SetDatabaseAdapter($this->db);
        try {
            ADODB_Active_Record::ClassHasMany('Profile', 'userid', 'profile_id', 'UserAR');
            $profile = new Profile();
            $profile->userid = new UserAR();
            if ($this->profile_id != '' AND $this->profile_id != NULL) {
                $profile->Load("id=$this->profile_id");
                $profile->update_user_id = $_SESSION['user_id'];
                $profile->is_delete = 1;
                $j = $profile->save();
            }
            if ($this->id != '' AND $this->id != NULL) {
                $profile->userid->Load("id=$this->id");
                $profile->userid->is_delete = 1;
                $profile->update_user_id = $_SESSION['user_id'];
                $i = $profile->userid->save();
            }
        } catch (exception $e) {
            //var_dump($e);
            print_r($e);
            adodb_backtrace($e->gettrace());
        }
        return $i;
    }

    public function getEmployOptions($sectionId) {
        $sql = "select id, full_name from aea_profile where section_id ='$sectionId'";
        $res = $this->db->Execute($sql);
        if ($res) {

            while (!$res->EOF) {
                $optionString .= "<option value=" . $res->fields['id'] . ">
                    " . $res->fields['full_name'] . "</option>";
                $posStart++;
                $res->MoveNext();
            }

        } else {
            $OptionString = mysql_errno() . ": " . mysql_error() . " at " . __LINE__ . " line in " . __FILE__ . " file<br>" . $sql;
        }

        return $optionString;
    }

}

require_once('libraries/adodb5/adodb-active-record.inc.php');

class EmployAR extends ADOdb_Active_Record {
    var $_table = 'aea_profile';

}

class EmpInsAR extends ADOdb_Active_Record {
    var $_table = 'aea_employ_institute';

}

